﻿using UnityEngine;
using System.Collections.Generic;

public class Snake 
{
    public int iDirectX = 0;
    public int iDirectY = 0;
    public float deltaSec = 0;
    public float moveSep = 0.3f;
    public bool bDirectionLock = false;
    public bool eat = false;
    public bool gameOver = false;
    public string body;
    public float fxOverTime;
    public int foodNum = 0;

    public void CreatSnake(int a, List<SnakeGrid> snakeGrid, List<List<Grid>> _lGrid, string _body) 
    {
        body = _body;
        for (int i = 0; i < 5; i++)
        {
            int x = 2 + i;
            int y = a;
            SnakeGrid sg = new SnakeGrid();
            sg.body = _body;
            sg.ix = x;
            sg.iy = y;
            snakeGrid.Insert(0, sg);
            _lGrid[x][y] = sg;
            iDirectX = 1;
            iDirectY = 0;
        }
    }

    public void Move(List<SnakeGrid> snakeGrid, List<SnakeGrid> snakeGrid2, List<List<Grid>> _lGrid, string _body, Vector2 _pos) 
    {
        //移动时间控制
        deltaSec += Time.deltaTime;
        if (deltaSec < moveSep) 
        {
            return;
        }
        deltaSec -= moveSep;
        
        //移动1
        Vector2 movePos = _pos;


        for(int i=0,size=snakeGrid.Count;i<size;i++)
        {
            SnakeGrid sg=snakeGrid[i];
            if (movePos.x == 0 || movePos.y == 0) 
            {
                movePos.x = sg.ix + iDirectX;
                movePos.y = sg.iy + iDirectY;
            }

            if (_lGrid[(int)movePos.x][(int)movePos.y] == snakeGrid2[snakeGrid2.Count - 1])
            {
                Debug.LogWarning("吃到别人的尾巴了");
                snakeGrid2.RemoveAt(snakeGrid2.Count - 1);
                SnakeGrid s = new SnakeGrid();
                s.ix = (int)movePos.x;
                s.iy = (int)movePos.y;
                snakeGrid.Insert(0, s);
                _lGrid[(int)movePos.x][(int)movePos.y] = s;
                s.body = _body;
                break;

            }
            if (_lGrid[(int)movePos.x][(int)movePos.y].isDead)
            {
                //gameOver = true;
                snakeGrid.RemoveAt(0);
                i--;
                size--;
                _lGrid[sg.ix][sg.iy] = new EmptyGrid();
                //Debug.LogWarning(size);
                break;
            }
            if (_lGrid[(int)movePos.x][(int)movePos.y] is FoodGrid)
            {
                FoodGrid fd = (FoodGrid)_lGrid[(int)movePos.x][(int)movePos.y];
                GameManager.gamemanager.lFoodGrid.Remove(fd);
                SnakeGrid s = new SnakeGrid();
                s.ix = (int)movePos.x;
                s.iy = (int)movePos.y;
                snakeGrid.Insert(0, s);
                _lGrid[(int)movePos.x][(int)movePos.y] = s;
                s.body = _body;

                eat = true;
                fd.Effect(body);
                foodNum++;

                //if (GameManager.gamemanager.speedCombo1 == true && _body == "1")
                //{
                //    GameManager.gamemanager.foodNumTime1 += 1f;
                //}
                //if (GameManager.gamemanager.speedCombo2 == true && _body == "2")
                //{
                //    GameManager.gamemanager.foodNumTime2 += 1f;
                //}

                if (foodNum >= 5)
                {
                    moveSep = 0.1f;
                    break;
                }
                else
                {
                    moveSep = 0.3f;
                    break;
                }

                //if (_body == "1")
                //{
                //    moveSep -= moveSep * 0.02f;
                //    break;
                //}
                //else
                //{
                //    moveSep -= moveSep * 0.02f;
                //    break;
                //}
            }

            Vector2 pos;
            pos.x = sg.ix;
            pos.y = sg.iy;
            _lGrid[sg.ix][sg.iy] = new EmptyGrid();
            sg.ix = (int)movePos.x;
            sg.iy = (int)movePos.y;
            _lGrid[sg.ix][sg.iy] = sg;
            movePos = pos;
        }
        bDirectionLock = false;
	}
}
